package com.grandtech.insurance;

import com.grandtech.gmis.common.filter.ContextFilter;
import com.grandtech.insurance.cache.CacheApi;
import com.grandtech.insurance.cache.CacheTileScheme;
import com.grandtech.insurance.common.core.bean.TileSchemeCore;
import com.grandtech.insurance.common.utils.FileUtil;
import com.grandtech.insurance.common.utils.IoXml;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.MultipartConfigElement;
import java.io.File;

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
@EnableAsync
public class NydbApplicationg {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(NydbApplicationg.class, args);
        initConfig();
    }

    /**
     * 拦截bean
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new ContextFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    private static void initConfig() throws Exception {
        String jarPath = CacheApi.getInstance().getJarRootPath();
        File file = new File(jarPath + File.separator + "config");
        if (!file.exists()) file.mkdirs();
        File[] schemes = file.listFiles();
        System.out.println("加载配置文件Path:" + file.getPath() + " 文件数:" + schemes.length);
        File tileSchemeFile;
        String tileSchemePath;
        for (int i = 0; i < schemes.length; i++) {
            tileSchemeFile = schemes[i];
            tileSchemePath = tileSchemeFile.getAbsolutePath();
            String schemeName = FileUtil.getFileNameByPath(tileSchemePath);
            TileSchemeCore tileSchemeCore = IoXml.readXmlFromPath(TileSchemeCore.class, tileSchemePath);
            CacheTileScheme.getInstance().cacheSchemeByName(schemeName, tileSchemeCore);
        }
    }

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.setMaxAge(18000L);
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    @Bean

    public MultipartConfigElement multipartConfigElement() {

        MultipartConfigFactory factory = new MultipartConfigFactory();

        //单个文件最大

        factory.setMaxFileSize("20MB");

        /// 设置总上传数据总大小

        factory.setMaxRequestSize("50MB");

        return factory.createMultipartConfig();

    }
}






















